package com.mini.demos.webgame.codec;

import org.apache.mina.core.buffer.IoBuffer;

import com.mini.demos.webgame.msg.AbstractGameMsg;
import com.mini.demos.webgame.msg.MsgHead;
import com.mini.framework.communicate.mina.protocol.Encoder;
import com.mini.framework.communicate.mina.protocol.Msg;

public abstract class AbstractEncoder implements Encoder{
	

	public IoBuffer encode(Msg msg) {
		AbstractGameMsg msgObj = (AbstractGameMsg)msg;
		IoBuffer buff = IoBuffer.allocate(msgObj.getHead().getTotalLength());
		buff.setAutoExpand(true);
		encodeHead(msgObj,buff);
		encodeBody(msgObj,buff);
		buff.flip();
		return buff;
	}
	
	public void encodeHead(AbstractGameMsg msg,IoBuffer buffer){
		MsgHead head = msg.getHead();
		buffer.putInt(head.getVersion());
		buffer.putInt(head.getCommandId());
		buffer.putInt(head.getSeqId());
		buffer.putInt(head.getTotalLength());
		buffer.putInt(head.getTime());
		buffer.putInt(head.getSpace());
	}
	
	public abstract void encodeBody(AbstractGameMsg msg,IoBuffer buffer);
	
	

	public abstract Class<? extends Msg> getMsgType();

}
